package com.softtown.modules.workflow.service.impl;

import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.modules.workflow.dao.SwfTaskDao;
import com.softtown.modules.workflow.dto.SwfTaskDTO;
import com.softtown.modules.workflow.entity.SwfTaskEntity;
import com.softtown.modules.workflow.service.SwfTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/8/22 14:41
 * @Description:
 */
@Service
public class SwfTaskServiceImpl extends BaseServiceImpl<SwfTaskDao, SwfTaskEntity> implements SwfTaskService {

    /**
     * 通过公司id和创建者id和流程实例找出对应的审批任务
     *
     * @param compId     公司id
     * @param creator    创建者id
     * @param procInstId 流程实例id
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 19:59
     */
    @Override
    public SwfTaskEntity getSwfTaskByCreator(Long compId, Long creator, Long procInstId) {
        return baseDao.getSwfTaskByCreator(compId, creator, procInstId);
    }

    /**
     * 撤销通过公司id和创建者id和流程实例找出对应的审批任务
     *
     * @param compId     公司id
     * @param creator    创建者id
     * @param procInstId 流程实例id
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 20:00
     */
    @Override
    public SwfTaskEntity getSwfTaskByCreatorByStatus(Long compId, Long creator, Long procInstId) {
        return baseDao.getSwfTaskByCreatorByStatus(compId, creator, procInstId);
    }

    /**
     * 更新审批任务
     *
     * @param swfTaskEntity 审批任务实体类
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 20:00
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSwfTask(SwfTaskEntity swfTaskEntity) {
        baseDao.updateSwfTask(swfTaskEntity);
    }

    /**
     * 获取待我审批中的审批上一审批人
     *
     * @param swfTaskDTO 审批任务DTO
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 20:01
     */
    @Override
    public SwfTaskEntity getswfHstAprvLast(SwfTaskDTO swfTaskDTO) {
        SwfTaskEntity swfTaskEntityReturn = new SwfTaskEntity();
        List<SwfTaskEntity> swfTaskEntities = baseDao.getTaskByProcInstId(swfTaskDTO.getCompId(), swfTaskDTO.getProcInstId());
        SwfTaskEntity swfTaskEntity = baseDao.getswfHstAprvLast(swfTaskDTO.getCompId(), swfTaskDTO.getAprvUserId(), swfTaskDTO.getProcInstId());
        swfTaskEntityReturn.setPhaseId(swfTaskEntity.getPhaseId());
        swfTaskEntityReturn.setIsPhaseLastAprv(swfTaskEntity.getIsPhaseLastAprv());
        swfTaskEntityReturn.setIsLastAprv(swfTaskEntity.getIsLastAprv());
        swfTaskEntityReturn.setCreator(swfTaskEntity.getCreator());
        if (swfTaskEntities.size() > 1) {
            SwfTaskEntity swfTaskEntity1 = baseDao.getswfHstAprvLastPosnName(swfTaskDTO.getCompId(), swfTaskEntity.getCreator(), swfTaskDTO.getProcInstId());
            if (swfTaskEntity1 != null) {
                swfTaskEntityReturn.setPosnName(swfTaskEntity1.getPosnName());
                swfTaskEntityReturn.setEmplName(swfTaskEntity1.getEmplName());
            } else {
                swfTaskEntityReturn.setPosnName("");
                swfTaskEntityReturn.setEmplName("");
            }
        } else {
            swfTaskEntityReturn.setPosnName("");
            swfTaskEntityReturn.setEmplName("");
        }
        return swfTaskEntityReturn;
    }

    /**
     * 通过公司id和流程实例找出对应的审批任务
     *
     * @param compId     公司id
     * @param procInstId 流程实例id
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 20:02
     */
    @Override
    public SwfTaskEntity getSwfTaskByApplByprocInstId(Long compId, Long procInstId) {
        return baseDao.getSwfTaskByApplByprocInstId(compId, procInstId);
    }

    /**
     * 获取当前任务审批段
     *
     * @param compId     公司id
     * @param procInstId 流程实例id
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 20:03
     */
    @Override
    public SwfTaskEntity getNowTaskPhase(Long compId, Long procInstId) {
        return baseDao.getNowTaskPhase(compId, procInstId);
    }

    /**
     * 获取当前审批人信息
     *
     * @param compId     公司id
     * @param procInstId 流程实例id
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 20:03
     */
    @Override
    public SwfTaskEntity getNewApproveInfo(Long compId, Long procInstId) {
        return baseDao.getNewApproveInfo(compId, procInstId);
    }

    /**
     * 获取审批上一审批人
     *
     * @param compId     公司id
     * @param aprvUserId 审批人id
     * @param procInstId 流程实例id
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 20:04
     */
    @Override
    public SwfTaskEntity getLastswfHstAprv(Long compId, Long aprvUserId, Long procInstId) {
        return baseDao.getswfHstAprvLast(compId, aprvUserId, procInstId);
    }

    /**
     * 获取最新任务
     *
     * @param compId     公司id
     * @param procInstId 流程实例id
     * @return com.softtown.modules.entity.SwfTaskEntity
     * @Author Dingyayong
     * @Date 2020/4/13 20:05
     */
    @Override
    public SwfTaskEntity getNewSwfTaskByApplByprocInstId(Long compId, Long procInstId) {
        return baseDao.getNewSwfTaskByApplByprocInstId(compId, procInstId);
    }

    @Override
    public List<SwfTaskEntity> getAllApprover(Long compId, Long procInstId,Integer applSeqNum) {
        return baseDao.getAllApprover(compId, procInstId,applSeqNum);
    }

    @Override
    public SwfTaskEntity getMaxApplSeqNum(Long compId, Long procInstId) {
        return  baseDao.getMaxApplSeqNum(compId, procInstId);
    }
}
